package cpre556.project2.eclipsePlugin.support;

import java.util.Vector;

public class Vulnerability {
	String filename;
	int lineNumber;
	String priority;
	String functionName;
	String lineOfCode;
	String description;
	String suggestion;
	
	public Vulnerability(){
		this.filename = null;
		this.lineNumber = 0;
		this.priority = null;
		this.functionName = null;
		this.lineOfCode = null;
		this.description = null;
		this.suggestion = null;
	}
	
	//////////Add by Qian///////////
	public boolean ignoredOrNot=false;
	
	/**
	 * Setters
	 */
	public void setFilename(String fn){
		filename = fn;
	}
	
	public void setLineNumber(int ln){
		lineNumber = ln;
	}
	
	public void setPriority(String p){
		priority = p;
	}
	
	public void setFunctionName(String fn){
		functionName = fn;
	}
	
	public void setLineOfCode(String loc){
		lineOfCode = getString(loc);
	}
	
	public void setDescription(String d){
		description = getString(d);
	}
	
	public void setSuggestion(String s){
		suggestion = s;
	}
	
	/**
	 * Getters
	 */
	public String getFilename(){
		return filename;
	}
	
	public int getLineNumber(){
		return lineNumber;
	}
	
	public String getPriority(){
		return priority;
	}
	
	public String getFunctionName(){
		return functionName;
	}
	
	public String getLineOfCode(){
		return getString(lineOfCode);
	}
	
	public String getDescription(){
		return getString(description);
	}
	
	public String getSuggestion(){
		return suggestion;
	}
	
	public Vector<String> getXMLSafeDescription(){
		return escapeXMLCharacters(description);
	}
	
	public Vector<String> getXMLSafeLineOfCode(){
		return escapeXMLCharacters(lineOfCode);
	}
	
	/**
	 * 
	 * @param str
	 * @return
	 */
	private String getString(String str){
		if(str.indexOf("&amp;") != -1){
			str = str.substring(0, str.indexOf("&amp;")) + "&" + str.substring(str.indexOf("&amp;")+5, str.length());
		}
		if(str.indexOf("&lt;") != -1){
			str = str.substring(0, str.indexOf("&lt;")) + "<" + str.substring(str.indexOf("&lt;") + 4, str.length());
		}
		if(str.indexOf("&gt;") != -1){
			str = str.substring(0,str.indexOf("&gt;"))+">"+str.substring(str.indexOf("&gt;")+4, str.length());
		}
		
		return str;
	}
	
	/**
	 * Create a Vector<String> for the given string
	 * @param str
	 * @return Vector<String>
	 */
	private Vector<String> toVector(String str){
		int strLength = str.length();
		Vector<String> returnVector = new Vector<String>();
				
		for(int i = 0; i < strLength; i++){
			returnVector.add(i, str.substring(i, i + 1));
		}
		
		return returnVector;
	}
	
	/**
	 * Create a Vector<String> with XML escaped characters
	 * @param xmlData
	 * @return
	 */
	protected Vector<String> escapeXMLCharacters(String xmlData){
		Vector<String> charXMLData = toVector(xmlData);
		Vector<String> escapedXMLData = new Vector<String>();
		
		int count = 0;
		
		for(int i = 0; i < charXMLData.size(); i++){
			if(charXMLData.get(i).toString().equalsIgnoreCase("&")){
				escapedXMLData.add(count, "&");
				count++;
				escapedXMLData.add(count, "a");
				count++;
				escapedXMLData.add(count, "m");
				count++;
				escapedXMLData.add(count, "p");
				count++;
				escapedXMLData.add(count, ";");
				count++;
			}
			else if(charXMLData.get(i).toString().equalsIgnoreCase("<")){
				escapedXMLData.add(count, "&");
				count++;
				escapedXMLData.add(count, "l");
				count++;
				escapedXMLData.add(count, "t");
				count++;
				escapedXMLData.add(count, ";");
				count++;
			}
			else if(charXMLData.get(i).toString().equalsIgnoreCase(">")){
				escapedXMLData.add(count, "&");
				count++;
				escapedXMLData.add(count, "g");
				count++;
				escapedXMLData.add(count, "t");
				count++;
				escapedXMLData.add(count, ";");
				count++;
			}			
			else{
				escapedXMLData.add(count, charXMLData.get(i));
				count++;
			}				
		}
		return escapedXMLData;
	}
	
	/**
	 * Compare Vulnerabilities
	 * @param ob
	 * @return
	 */
	public boolean compareTo(Object ob){
		Vulnerability vul = (Vulnerability)ob;
		if(!this.description.equals(vul.description)){
			return false;
		}
		if (!this.filename.equals(vul.filename)){
			return false;
		}
		if(!this.functionName.equals(vul.functionName)){
			return false;
		}
		if(this.lineNumber != vul.lineNumber){
			return false;
		}
		if(!this.suggestion.equals(vul.suggestion)){
			return false;
		}
		return true;
	}
}
